<?php

namespace Admin\Controller;

class GoodsBrandController extends CommonController{
	
	protected function _initialize(){
		parent::_initialize();
			$this->breadcrumb1='商品';
			$this->breadcrumb2='商品品牌';
	}
	
	public function index(){
		
		$sql='SELECT id,goods_class_id,name FROM '
		.C('DB_PREFIX').'goods_brand';
		
		$cate = M()->query($sql);
		$list =list_to_tree($cate);
		$this->list=json_encode($list);
		//查询分类
		$category_sql='SELECT id,pid,name FROM ' .C('DB_PREFIX').'goods_category';
		$category = M()->query($category_sql);
		$this->assign('category',$category);
		$this->display();
	}
	function add(){
		
		if(IS_POST){
			$d['name']=I('name');
			$d['goods_class_id']=I('goods_class_id');
			$d['brand_logo']=I('image');
			$d['brand_description']=I('brand_description');
			$d['recommend']=I('recommend');
			$d['create_time']=time();
			$d['letter']=I('letter');
			$d['brand_banner']=I('brand_banner');
			$d['sort_order']=I('sort_order');
			if(M('goods_brand')->where(array('name'=>$d['name'],'goods_class_id'=>$d['goods_class_id']))->find()){
				$data['err']='该品牌名称已经存在';
				$this->ajaxReturn($data);				
				die();
			}
			
			$id=M('goods_brand')->add($d);
			if($id){
				
				$data['name'] =$d['name'];			
				$data['id']=$id;
				$this->ajaxReturn($data);
				
				die();
			}else{
				
				die();
			}
		}

	}
	
		function edit(){
		if(IS_POST){

			$d['id']=I('id');
			$d['name']=I('name');
			if(!empty(I('goods_class_id'))){
				$d['goods_class_id']=I('goods_class_id');
			}
			if(!empty(I('image'))){
				$d['brand_logo']=I('image');
			}
			$d['brand_description']=I('brand_description');
			$d['recommend']=I('recommend');
			$d['create_time']=time();
			$d['letter']=I('letter');
			$d['brand_banner']=I('brand_banner');
			$d['sort_order']=I('sort_order'); ;
			$category=M('goods_brand')->find($d['id']);

			if(M('goods_brand')->where(array('name'=>$d['name'],'goods_class_id'=>$category['goods_class_id']))->find()){
				$data['err']='该分类名称已经存在';				
				$this->ajaxReturn($data);				
				die();
			}
			
			
			$r=M('goods_brand')->save($d);
			
			if($r){
				
				$data['success']='修改成功';
				$data['name']=$d['name'];
				$this->ajaxReturn($data);
								
				die();
			}else{
				
				$data['err']='修改失败';
				
				$this->ajaxReturn($data);				
				
				die();
			}
		}
	}

	//品牌详细信息
	function get_info(){
		if(IS_POST){
			$id=I('id');
			$d=M('goods_brand')->find($id);
			$data['name']=$d['name'];
			$data['brand_description']=$d['brand_description'];
			$data['recommend']=$d['recommend'];
			$data['letter']=$d['letter'];
			$data['brand_banner']=$d['brand_banner'];
			$data['sort_order']=$d['sort_order'];

			//logo展示
			if(!empty($d['brand_logo'])){
				$data['image']=resize($d['brand_logo'], 100, 100);
			}else{
				$data['image']='/Common/image/no_image_100x100.jpg';
			}

			//根据分类id找到分类
			$where = array(
				'id'=>$d['goods_class_id'],
			);
			$category_name = M('goods_category')->where($where)->field('name')->find();
			$data['cname']=$category_name['name'];
			$this->ajaxReturn($data);
		}
	}
	//删除品牌
	function del(){
		if(IS_POST){
			$id=I('id');
			
			/*if(M('goods_brand')->where('pid='.$id)->find()){
				$data['err']='请先删除子节点！！';
				$this->ajaxReturn($data);
				die;
			}*/
			if(M('goods')->where(array('brand_id'=>$id))->find()){
				$data['err']='请先删除该品牌下商品！！';
				$this->ajaxReturn($data);
				die;
			}
			
			if(M('goods_brand')->where('id='.$id)->delete()){
				$data['success']='删除成功';
				$this->ajaxReturn($data);
				die();
			}
		}		
	}	
	
	function autocomplete(){
		$json = array();
		
		$filter_name=I('filter_name');
		
		if (isset($filter_name)) {
			$sql='SELECT id,name FROM '.c('DB_PREFIX')."goods_brand where name LIKE'%".$filter_name."%' LIMIT 0,20";
		}else{
			$sql='SELECT id,name FROM '.c('DB_PREFIX')."goods_brand LIMIT 0,20";
		
		}
			$results = M('goods_brand')->query($sql);

		foreach ($results as $result) {
			$json[] = array(
				'category_id' => $result['id'],
				'name'        => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
			);
		}
			
		$this->ajaxReturn($json);
	}
	
}
?>